package com.yzt.message;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yzt.zhuju.arouter.RouterMain;
import com.yzt.zhuju.utils.UrlUtil;

import io.rong.push.PushType;
import io.rong.push.notification.PushMessageReceiver;
import io.rong.push.notification.PushNotificationMessage;

import static com.yzt.zhuju.rongyun.IMMessageType.SERVER_NOTICE_MESSAGE;

public class RongPushMessageReceiver extends PushMessageReceiver {

    @Override
    public boolean onNotificationMessageArrived(Context context, PushType pushType, PushNotificationMessage message) {
        Log.e("TGA", "NOTIFY_MESSAGE-----onNotificationMessageArrived");
        return false;
    }

    @Override
    public boolean onNotificationMessageClicked(Context context, PushType pushType, PushNotificationMessage message) {
        Log.e("TGA", "NOTIFY_MESSAGE-----onNotificationMessageClicked");
        if (message.getTargetId().equals(MessageConstants.TYPE_PUSH_NOTICE)) {
            RouterMain.MainActivity();
            return true;
        }

        //服务器通知消息，使用的用户id是声网id，不能让融云处理
        if (message.getObjectName().equals(SERVER_NOTICE_MESSAGE)) {
            RouterMain.MainActivity();
            return ClickOffLineMsg(message, pushType);
        }
        return false;
    }

    private boolean ClickOffLineMsg(PushNotificationMessage message, PushType pushType) {
        Log.e("TGA", "NOTIFY_MESSAGE-----ClickOffLineMsg");
        try {
            String pushData = message.getPushData();
            if (!TextUtils.isEmpty(pushData)) {
                JSONObject js = JSON.parseObject(pushData);
                JSONObject appData = js.getJSONObject("appData");
                String jumpUrl = appData.getJSONObject("data").getString("jumpUrl");
                UrlUtil.turnToARoutePage4JO(JSONObject.parseObject(jumpUrl));
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public void onThirdPartyPushState(PushType pushType, String action, long resultCode) {
        Log.e("TGA", "NOTIFY_MESSAGE-----onThirdPartyPushState");
        super.onThirdPartyPushState(pushType, action, resultCode);
    }
}
